package com.guoning.office_system.controller;

import com.alibaba.fastjson.JSONArray;
import com.guoning.office_system.model.User;
import com.guoning.office_system.model.WorkOrder;
import com.guoning.office_system.service.WorkOrderService;
import com.guoning.office_system.utils.Constants;
import com.guoning.office_system.utils.JsonResult;
import com.guoning.office_system.vo.Criteria;
import com.guoning.office_system.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/workOrder")
public class WorkOrderController {

    @Autowired
    private WorkOrderService workOrderService;

    /**
     * 获取标签数量
     * @return
     */
    @RequestMapping(value = "/getItemAndCount")
    public JsonResult getItemAndCount(HttpServletRequest request){
        JsonResult js;
        try {
            User user = (User) request.getSession().getAttribute(request.getHeader("Token"));
            List list = workOrderService.getEnableStatusItemAndCount(user.getId());
            js = new JsonResult(Constants.STATUS_SUCCESS, "获取标签数量成功", list);
        }catch (Exception e){
            js = new JsonResult(Constants.STATUS_ERROR, "获取标签数量异常");
        }
        return js;
    }

    /**
     * 获取待办工单
     * @param request
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/selectByQueryCondition")
    public JsonResult selectByQueryCondition(HttpServletRequest request,
                               @RequestParam(value = "pageIndex", defaultValue = "0") Integer pageIndex,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "departmentId", defaultValue = "") Long departmentId,
                               @RequestParam(value = "enableStatus", defaultValue = "") Integer enableStatus,
                               @RequestParam(value = "orderCodeOrTitleOrContent", defaultValue = "") String orderCodeOrTitleOrContent){
        JsonResult js;
        if(enableStatus == 0){
            enableStatus = null;
        }
        try {
            User user = (User) request.getSession().getAttribute(request.getHeader("Token"));
            PageBean pageBean = new PageBean();
            pageBean.setPageIndex(pageIndex);
            pageBean.setPageSize(pageSize);
            Criteria criteria = new Criteria();
            criteria.addExtField("userId", user.getId());
            criteria.addExtField("departmentId", departmentId);
            criteria.addExtField("enableStatus", enableStatus);
            criteria.addExtField("orderCodeOrTitleOrContent", orderCodeOrTitleOrContent);
            pageBean = workOrderService.selectByQueryCondition(criteria, pageBean);
            js= new JsonResult(Constants.STATUS_SUCCESS, "查询成功", pageBean);
        }catch (Exception ex){
            js = new JsonResult(Constants.STATUS_ERROR, "查询异常", ex.getMessage());
        }
        return js;
    }

    /**
     * 标为已阅
     * @param params
     * @return
     */
    @RequestMapping(value = "/updateHaveRead", method = RequestMethod.POST)
    public JsonResult updateHaveRead(String params){
        JsonResult js = null;
        try {
            List<WorkOrder> workOrderList = JSONArray.parseArray(params, WorkOrder.class);
            int i = workOrderService.updateHaveRead(workOrderList);
            js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "操作失败");
        }
        return js;
    }

    /**
     * 标为已办
     * @param params
     * @return
     */
    @RequestMapping(value = "/updateHaveDone", method = RequestMethod.POST)
    public JsonResult updateHaveToDo(String params){
        JsonResult js;
        try {
            List<WorkOrder> workOrderList = JSONArray.parseArray(params, WorkOrder.class);
            int i = workOrderService.updateHaveToDo(workOrderList);
            js = new JsonResult(Constants.STATUS_SUCCESS, "操作成功");
        }catch (Exception ex){
            js = new JsonResult(Constants.STATUS_ERROR, "操作失败");
        }
        return js;
    }

    /**
     * 删除工单
     * @param id
     * @return
     */
    @RequestMapping(value = "/dalete", method = RequestMethod.POST)
    public JsonResult delete(Long id){
        JsonResult js;
        try{
            int i = workOrderService.updateForDeleteById(id);
            if(i != 0){
                js = new JsonResult(Constants.STATUS_SUCCESS,"操作成功");
            }else{
                js = new JsonResult(Constants.STATUS_FAIL, "操作失败");
            }
        }catch (Exception e){
            js = new JsonResult(Constants.STATUS_ERROR, "操作异常");
        }
        return js;
    }
}
